package com.lmh;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Liumh on 2018/5/25.
 */
public class JsoupParser {

    private static String CAT_BASE = "http://www.520pcat.com/";
    private static List<String> monthHtmls = new ArrayList<String>();
    private static List<String> dayHtmls = new ArrayList<String>();
    private static List<String> fileHtmls = new ArrayList<String>();

    public static void main(String[] args){
        try {
            /*JsoupParser jsoupParser = new JsoupParser();
            jsoupParser.topParser(CAT_BASE+"upload",monthHtmls);*/
            catelogHandler();
            /*List<String> list = getContentByTag(CAT_BASE+"upload","a");
            System.out.println(list);*/
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 文档解析
     * @param doc
     */
    private static void topParser(String urlStr, List<String> htmls) throws Exception{
        URL url = new URL(urlStr);
        Document doc = Jsoup.parse(url,1000);
        Element element = doc.body();
        Elements elements = element.children();
        hierarchyHandler(element, htmls);
        System.out.println(htmls.size()+"："+htmls.toString());
    }

    /**
     * 文件解析
     */
    private static void bottomParser(){

    }

    private static List<String> getContentByTag(String url, String tagName) throws Exception{
        URL execURL = new URL(url);
        Document document = Jsoup.parse(execURL,20000);
        List<String> list = new ArrayList<String>();
        Elements elements = document.getElementsByTag(tagName);
        Iterator<Element> elementIterator = elements.iterator();
        while (elementIterator.hasNext()){
            Element element = elementIterator.next();
            if(!element.html().contains("[To Parent Directory]")){
                list.add(element.html());
            }
        }
        return list;
    }

    /**
     * 文档层级结构控制
     */
    private static void hierarchyHandler(Element element, List<String> htmls){
        Elements elements = element.children();
        String eleHtml = element.html();
        if (elements.isEmpty() &&
                !eleHtml.equals("[To Parent Directory]") &&
                !eleHtml.trim().equals("")){
            htmls.add(element.html());
        }else{
            Iterator<Element> elementIterator = elements.iterator();
            while (elementIterator.hasNext()){
                Element ele = elementIterator.next();
                hierarchyHandler(ele, htmls);
            }
        }
    }

    /**
     * 网站目录结构处理
     */
    private static void catelogHandler() throws Exception{
        String url = CAT_BASE+"upload";
        List<String> months = getContentByTag(url,"a");
        for (String month:months) {
            List<String> days = getContentByTag(url+"/"+month,"a");
//            System.out.println(month);
            for (String day:days) {
                List<String> files = getContentByTag(url+"/"+month+"/"+day,"a");
//                System.out.println(day);
                for (String file:files) {
                    if(file.contains("txt")){
//                        System.out.println(url+"/"+month+"/"+day+"/"+file);
                        String doClient = doClient(url+"/"+month+"/"+day+"/"+file);
                        System.out.println(doClient.replace(" ",""));
//                        return;
                    }
                }
            }
        }
    }

    private static String doClient(String url) throws Exception{
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseMsg = EntityUtils.toString(entity,"GBK");
        response.close();;
        return responseMsg;
    }
}
